<template>
    <div class="edu-page-warp">
        <!-- <div class="edu-page-title new">
            <span class="active">课程门数</span>
        </div> -->
        <div class="coursemap">
            <span style="color:#C0C4CC">培养方案模板规则</span>
            <span style="color:#C0C4CC">/</span>
            <span style="color:#909399">规则详情</span>
        </div>
        <div class="bg-white pd-20px" key="tab1">
            <!-- <el-row :gutter="4" style="line-height:24px;">
                <el-col :span="1">
                    <i class="el-icon-info float-right secondary-text" style="margin-top:4px;"></i>
                </el-col>
                <el-col :span="23">
                    <span v-for="item in infos" :key="item" class="secondary-text font-size-14">
                        {{item}}
                        <br>
                    </span>
                </el-col>
            </el-row> -->
            <el-row>
                 <el-col :span="2" class="float-right">
                    <el-button plain size="mini" @click="addCourseRule()">新增</el-button>
                </el-col>
            </el-row>
            <el-row class="pd-20px">
                <el-col>
                    <el-table :data="rulesList" stripe size="mini" class="border-top">
                        <el-table-column type="index" label="序号" width="80"></el-table-column>
                        <el-table-column label="规则名称" min-width="120">
                            <template slot-scope="scope">
                                <el-popover
                                    :content="scope.row.name"
                                    placement="bottom-start"
                                    trigger="hover"
                                >
                                    <span slot="reference">
                                        <el-button
                                            type="text"
                                            @click="preview(scope.row)"
                                        >{{ scope.row.name }}</el-button>
                                    </span>
                                </el-popover>
                            </template>
                        </el-table-column>
                        <!-- <el-table-column label="课程模块最多门数" min-width="120">
                            <template slot-scope="props">
                                <span>{{props.row.maxNum}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="课程模块最少门数" min-width="120">
                            <template slot-scope="props">
                                <span>{{props.row.minNum}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="学分限制类型" min-width="120">
                            <template slot-scope="props">
                                <span>{{props.row.expression ? (props.row.expression === 1 ? "大于等于" : "等于") : ""}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="学分限制" min-width="120">
                            <template slot-scope="props">
                                <span >{{props.row.minCredits}}</span>
                            </template>
                        </el-table-column> -->
                        <el-table-column label="操作" width="150">
                            <template slot-scope="scope">
                                <el-button
                                    type="text"
                                    @click="showCourseLabelBox(scope.row)"
                                >{{$t('message.edit')}}</el-button>
                                <el-button
                                    type="text"
                                    @click="deleteRule(scope.row)"
                                >{{$t('message.delete')}}</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-row>
                        <el-col>
                            <el-pagination
                                small
                                @size-change="handleSizeChange"
                                @current-change="handlePageNumChange"
                                :current-page="queryRuleForm.pageNum_"
                                :page-sizes="[20,50,100,500,1000]"
                                :page-size="queryRuleForm.pageSize_"
                                layout="total, prev, pager, next"
                                :total="queryRuleForm.total"
                                style="float: right;margin:10px 60px 0 0;"
                            ></el-pagination>
                        </el-col>
                    </el-row>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="22">
                    <div class="empty-box"></div>
                </el-col>
                <el-col :span="2">
                    <el-button type="primary" @click="ok">确定</el-button>
                </el-col>
            </el-row>
        </div>
        <!-- 新增 -->
        <!-- <el-dialog title="编辑规则" width="40%" :visible.sync="showCourseLabelDialog" :close-on-click-modal="false"
            :close-on-press-escape="false">
            <el-form ref="form" :model="curRule" label-width="80px" inline>
                <el-row>
                    <el-col span="20" :offset='3'>
                        <el-form-item label="课程类别:">
                            <el-select
                                v-model="curRule.label"
                                filterable
                                remote
                                placeholder="请输入（支持模糊查询）"
                                :remote-method="findcourseLab"
                                style="width: 100%;"
                                clearable
                            >
                                <el-option
                                    v-for="item in options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                                <i class="el-icon-search" slot="prefix"></i>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col span="1" :offset='2'>
                        <el-radio v-if="curRule.id" v-model="limitType" style="margin-top:8px;" label="0">&nbsp;</el-radio>
                        <el-checkbox v-else v-model="checkedNum" style="margin-top:8px;"></el-checkbox>
                    </el-col>
                    <el-col span="19">
                        <el-form-item prop="optional" label="至少门数:">
                            <el-input-number
                                v-model="curRule.minNum"
                                @change="optionalChange"
                                :min="0"
                                :max="100"
                                size="mini"
                                controls-position="right"
                                style="width:95px"
                                >
                            </el-input-number>
                            
                               
                            <span>-</span>
                            <el-input-number
                                v-model="curRule.maxNum"
                                @change="totalChange"
                                :min="0"
                                :max="100"
                                size="mini"
                                controls-position="right"
                               style="width:95px"
                                
                            ></el-input-number>
                            
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col span="1" :offset='2'>
                        <el-radio v-if="curRule.id" v-model="limitType" style="margin-top:8px;" label="1">&nbsp;</el-radio>
                        <el-checkbox v-else v-model="checkedCredits" style="margin-top:8px;"></el-checkbox>
                    </el-col>
                    <el-col span="19">
                        <el-form-item prop="optional" label="学分限制:">
                            <el-select v-model="curRule.expression" style="width:95px">
                                <el-option
                                    v-for="item in expressions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                            
                        </el-form-item>
                        <el-form-item prop="totalNum">
                            <el-input
                                v-model="curRule.minCredits"
                                style="width:95px"
                                 placeholder="学分数"
                            ></el-input>
                            
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer">
                <el-button
                    @click="showCourseLabelDialog = false"
                    size="mini"
                >{{$t('message.cancel')}}</el-button>
                <el-button
                    @click="saveCourseRule"
                    type="primary"
                    size="mini"
                >{{$t('message.confirm')}}</el-button>
            </div>
        </el-dialog> -->
        <el-dialog :title="title" width="40%" :visible.sync="showCourseLabelDialog" :close-on-click-modal="false"
            :close-on-press-escape="false">
            <el-form ref="form" :model="curRule" label-width="80px" inline :rules="rules">
                <el-row>
                    <el-col span="24">
                        <el-form-item label="规则名称:" prop="name">
                            <el-input
                                v-model="curRule.name"
                                placeholder="请输入规则名称"
                                :disabled="disabled"
                                maxlength="50"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col span="24">
                        <div class="clearfix" style="margin:10px 0;">
                            <div class="float-left">
                                <h2>课程性质</h2>
                            </div>
                            <div class="float-right" v-if="!disabled">
                                <el-button
                                    @click="addCourseType"
                                    size="mini"
                                >添加行</el-button>
                            </div>
                        </div>
                        <el-table :data="courseType" stripe size="mini" class="border-top">
                            <el-table-column label="公选课类别" min-width="120">
                                <template slot-scope="props">
                                    <el-select v-model="props.row.labID"  :disabled="!props.row.edit" size="mini">
                                        <el-option
                                            v-for="item in courseListType"
                                            :key="item.labid"
                                            :label="item.namecn"
                                            :value="item.labid"
                                        />
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column label="模块上限门数" min-width="120">
                                <template slot-scope="props">
                                    <el-input v-if="props.row.edit" oninput="if(value.length>4)value=value.slice(0,4)" v-model="props.row.maxNum" size="mini" type="number" :min="0"/>
                                    <span v-else>{{ props.row.maxNum }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="操作" min-width="120" v-if="!disabled">
                                <template slot-scope="props">
                                    <el-button
                                        type="text"
                                        v-if="props.row.edit"
                                        @click="showCourseSava(props.$index)"
                                    >保存</el-button>
                                    <el-button
                                        type="text"
                                        v-if="!props.row.edit"
                                        @click="showCourseEdit(props.$index,props.row)"
                                    >编辑</el-button>
                                    <el-button
                                        type="text"
                                        @click="showCourseDelelte(props.$index)"
                                    >删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col span="24">
                        <h2 style="margin: 20px 0 10px;">例外</h2>
                    </el-col>
                    <el-col span="11">
                        <el-form-item label="4年制已选:">
                            <el-input
                                v-model="curRule.four"
                                size="mini" type="number" :min="0"
                                oninput="if(value.length>4)value=value.slice(0,4)"
                                :disabled="disabled"
                                style="width:80%"
                            ></el-input>
                            <span style="margin-left:5px">学分</span>
                        </el-form-item>
                    </el-col>
                    <el-col span="11" offset="2">
                        <el-form-item label="5年制已选:">
                            <el-input
                                v-model="curRule.five"
                                size="mini" type="number" :min="0"
                                oninput="if(value.length>4)value=value.slice(0,4)"
                                :disabled="disabled"
                                style="width:80%"
                            ></el-input>
                            <span style="margin-left:5px">学分</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                 <el-row>
                    <el-col span="24">
                        <div class="clearfix" style="margin:10px 0;">
                            <div class="float-left">
                                <h2>标签</h2>
                            </div>
                            <div class="float-right">
                                <el-button
                                    @click="addCourseTag"
                                    v-if="!disabled"
                                    size="mini"
                                >添加行</el-button>
                            </div>
                        </div>
                        <el-table :data="courseTag" stripe size="mini" class="border-top">
                            <el-table-column label="标签" min-width="120">
                                <template slot-scope="props">
                                    <el-select v-model="props.row.tagID" :disabled="!props.row.edit"  size="mini">
                                        <el-option
                                            v-for="item in coutseTag"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value"
                                        />
                                    </el-select>
                                    <!-- <span v-else>{{ props.row.tagID }}</span> -->
                                </template>
                            </el-table-column>
                            <el-table-column label="至少门数" width="80">
                                <template slot-scope="props">
                                    <el-input v-if="props.row.edit" v-model="props.row.minNum" oninput="if(value.length>4)value=value.slice(0,4)" size="mini" type="number" :min="0"/>
                                    <span v-else>{{ props.row.minNum }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="至多门数" width="80">
                                <template slot-scope="props">
                                    <el-input v-if="props.row.edit" v-model="props.row.maxNum" oninput="if(value.length>4)value=value.slice(0,4)" size="mini" type="number" :min="0"/>
                                    <span v-else>{{ props.row.maxNum }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="至少学分" width="80">
                                <template slot-scope="props">
                                    <el-input v-if="props.row.edit" v-model="props.row.minCredits" oninput="if(value.length>4)value=value.slice(0,4)" size="mini" type="number" :min="0"/>
                                    <span v-else>{{ props.row.minCredits }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="至多学分" width="80">
                                <template slot-scope="props">
                                    <el-input v-if="props.row.edit" v-model="props.row.maxCredits" oninput="if(value.length>4)value=value.slice(0,4)" size="mini" type="number" :min="0"/>
                                    <span v-else>{{ props.row.maxCredits }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="操作" min-width="120" v-if="!disabled">
                                <template slot-scope="props">
                                    <el-button
                                        type="text"
                                        v-if="props.row.edit"
                                        @click="showTagSava(props.$index)"
                                    >保存</el-button>
                                    <el-button
                                        type="text"
                                        v-if="!props.row.edit"
                                        @click="showTagEdit(props.$index)"
                                    >编辑</el-button>
                                    <el-button
                                        type="text"
                                        @click="showTagDelelte(props.$index)"
                                    >删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer">
                <el-button
                    @click="cancel"
                    size="mini"
                >{{$t('message.cancel')}}</el-button>
                <el-button
                    @click="saveCourseRule"
                    type="primary"
                    size="mini"
                >{{$t('message.confirm')}}</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import {
    // queryElectiveCourseList,
    deleteCultureRule,
    addCultureRule,
    updateCultureRule
} from "common/src/api/culture/cultureSchemeRule";
import {
    findCultureRex,
    findTamPleLab,
    inserBclCultureRex,
    updateCultureRex,
    findCourseTagNameByGrade,
    findPublicElectiveByGrade,
    insertBclCultureRule,
    findBclCultureRule,
    findBclCultureRuleDetail,
    updateBclCultureRule,
    deleteBclCultureRule
    // findcourseLab
} from "common/src/api/culture/bachelor";
export default {
    name: "bclCultureSchemeRule",
    data() {
        return {
            activeTab: "tab1",
            // infos: [
            //     "1、选修2学分创新创业的通识选修课程",
            //     "2、每个模块最多选修两门课程(通识选修课分为人文经典与审美素养、工程能力与创新思维、社会发展与国际视野、科学探索与生命关怀四大模块)",
            //     "3、至少选修一门精品类通识选修课(核心通识课程、同济烙印课程、长青系列课程、交叉融通课程、校级精品通识课程将用课程标签予以标注)"
            // ],
            rulesList: [],
            tempCourse: { labelId: "", totalNum: "1", optional: "1" },
            curRule: {
                name:"",
                templateID:"",
                five:"",
                four:"",
                publicElectiveRule:[],
                courseTagRule:[]
            },
            totalLang:[],
            optionalCourses: [],
            electiveCourses: [],
            maxNumber:1,
            items: [],
            queryRuleForm: {
                pageSize_: 20,
                pageNum_: 1,
                total: 0,
                templateID: this.$route.query.id
            },
            showCourseLabelDialog: false,
            courseType: [
                {edit:true},
            ],
            courseTag: [
                {edit:true},
            ],
            courseRow:{
                type:"",
                num:"",
            },
            tagRow:{
                type:"",
                num:"",
            },
            disabled:false,
            limitType: 0,
            checkedNum:true,
            checkedCredits:false,
            title:'',
            id:'',
            options:[],
            rules: {
                name: [
                    { required: true, message: '请输入规则名称', trigger: 'blur' },
                ],
            },
            coutseTag:[],
            courseListType:[],
            ruleId:""
            // checkedNum:false,
            // checkedCredits:false,
        };
    },
    created() {
        //this.toggle();
        //this.activeTab = this.$route.query.projId;
        this.id =  this.$route.query.id
        this.queryRules();
    },
    mounted(){
        // this.getInfos();
    },
    methods: {
        // getInfos(rules){
        //     let ruleMap=rules.map(item=>{
        //         return {
        //             id:item.id,
        //             label:item.label,
        //             minCredits:item.minCredits,
        //             optional:item.optional,
        //             totalNum:item.totalNum
        //         }
        //     })

        //     let num=ruleMap.filter(item=>{
        //         return item.optional&&item.totalNum
        //     })

        //     // let optional=rules.filter(item=>{//最少
        //     //     return item.optional&&!item.totalNum
        //     // })
        //     // let totalNum=rules.filter(item=>{//最多
        //     //     return (!item.optional)&&item.totalNum
        //     // })
        //     console.log('num',ruleMap);
        // },
        //查看规则
        preview(row){
            this.title = "查看规则"
            this.showCourseLabelDialog = true;
            this.disabled = true;
            this.ruleId = row.id;
            this.getRuleDetails();
            this.curRule = Object.assign({}, row);
        },
        cancel(){
            this.showCourseLabelDialog = false;
            this.clearForm();
            this.disabled = false;
        },
        //新增规则
        addCourseType(){
            this.courseType.push({edit:true})
        },
        //查询标签
        findCourseTagName(){
            let year =  this.$route.query.year
            let trainingLevel =  this.$route.query.trainingLevel
            if(trainingLevel == "本科"){
                trainingLevel = 1
            }
            if(trainingLevel == "专科"){
                trainingLevel = 2
            }
            if(trainingLevel == "专升本"){
                trainingLevel = 3
            }
            if(trainingLevel == "预科"){
                trainingLevel = 5
            }
            if(trainingLevel == "第二学士学位"){
                trainingLevel = 7
            }
            findCourseTagNameByGrade(year,trainingLevel).then(res => {
                if (res.code == 200) {
                    let data = res.data.map(item => {
                            return {
                                value: item.value*1,
                                label: item.label,
                            };
                    });
                   this.coutseTag = data
                }
            });
        },
        //查询课程类别
        findPublicElectiveByGrade(){
            let year =  this.$route.query.year
            let trainingLevel =  this.$route.query.trainingLevel
            if(trainingLevel == "本科"){
                trainingLevel = 1
            }
            if(trainingLevel == "专科"){
                trainingLevel = 2
            }
            if(trainingLevel == "专升本"){
                trainingLevel = 3
            }
            if(trainingLevel == "预科"){
                trainingLevel = 5
            }
            if(trainingLevel == "第二学士学位"){
                trainingLevel = 7
            }
            findPublicElectiveByGrade(year,trainingLevel).then(res => {
                if (res.code == 200) {
                   this.courseListType = res.data
                }
            });
        },
        //保存课程类别
        showCourseSava(index){
            if(!this.courseType[index].maxNum || !this.courseType[index].labID){
                this.$message({
                    type: "warning",
                    message: "请先填写完整再保存"
                });
                return false;
            }else{
                if(this.courseType.length > 0){
                    let num = 0
                    this.courseType.forEach(d =>{
                        if(d.labID == this.courseType[index].labID){
                            num++
                        }
                    })
                    if(num > 1){
                        this.$message({
                            type: "warning",
                            message: "该公选课类别重复，请重新选择"
                        });
                        return false;
                    }else{
                        this.courseType[index].edit = false;
                    }
                }else{
                    this.courseType[index].edit = false;
                }
                
            }
        },
        //删除课程类别
        showCourseDelelte(index){
            this.courseType.splice(index, 1);
        },
        //编辑课程类别
        showCourseEdit(index,row){
            this.courseType[index].edit = true;
            //Vue.set(this.courseType, index, Object.assign(edit, row));     
           
        },
        //新增标签
        addCourseTag(){
            this.courseTag.push({edit:true})
        },
        //保存标签
        // showTagSava(index){
        //     if(!this.courseTag[index].tagID || !(this.courseTag[index].minNum || this.courseTag[index].maxNum || this.courseTag[index].minCredits || this.courseTag[index].maxCredits)){
        //         this.$message({
        //             type: "warning",
        //             message: "请先填写完整再保存"
        //         });
        //         return false;
        //     }else{
        //         this.courseTag[index].edit = false;
        //     }
        // },
        showTagSava(index){
            if(!this.courseTag[index].tagID || !(this.courseTag[index].minNum || this.courseTag[index].maxNum || this.courseTag[index].minCredits || this.courseTag[index].maxCredits)){
                this.$message({
                    type: "warning",
                    message: "请先填写完整再保存"
                });
                return false;
            }else{
                if(this.courseTag[index].minNum*1 > this.courseTag[index].maxNum*1){
                    this.$message({
                    type: "warning",
                    message: "至少门数不能大于至多门数"
                });
                return false;
                }
                if(this.courseTag[index].minCredits*1 > this.courseTag[index].maxCredits*1){
                    this.$message({
                    type: "warning",
                    message: "至少学分不能大于至多学分"
                });
                return false;
                }
                if(this.courseTag.length > 0){
                    let num = 0
                    this.courseTag.forEach(d =>{
                        if(d.tagID == this.courseTag[index].tagID){
                            num++
                        }
                    })
                    if(num > 1){
                        this.$message({
                            type: "warning",
                            message: "该公选课类别重复，请重新选择"
                        });
                        return false;
                    }else{
                        this.courseTag[index].edit = false;
                    }
                }else{
                    this.courseTag[index].edit = false;
                }
                
            }
        },
        //删除标签
        showTagDelelte(index){
            this.courseTag.splice(index, 1);
        },
        //编辑标签
        showTagEdit(index){
            this.courseTag[index].edit = true;
        },
        // 查询课程类型
        findcourseLab(val){
             let id = {
                templateId: this.id,
                condition: val
            };
            console.log(id)
            findTamPleLab(id).then(res=>{
                if(res.code==200){
                    let data = res.data.map(item => {
                        return {
                            value: item.id,
                            label:
                                "en" === this.$store.getters.language
                                    ? item.labelEn
                                    : item.label,
                            // parentID: item.parentid,
                            // labtype:item.labtype,
                        };
                    });
                    this.options = data;
                }
            })
        },
        //查询规则详情列表
        getRuleDetails(){
            findBclCultureRuleDetail(this.ruleId).then(res => {
                if (res.code == 200) {
                    let courseTag = res.data.courseTagRuleList;
                    let courseType = res.data.publicElectiveRuleList;
                    this.courseType = courseType.map(d =>{
                        return{
                            edit: false,
                            labID: d.labID,
                            maxNum: d.maxNum
                        }
                    })
                    this.courseTag = courseTag.map(d =>{
                        return{
                            edit: false,
                            tagID: d.tagID,
                            minNum: d.minNum,
                            maxNum: d.maxNum,
                            minCredits: d.minCredits,
                            maxCredits: d.maxCredits,
                        }
                    })
                }else{
                    this.courseTag = [];
                    this.courseType = [];
                }
            })
        },
        showCourseLabelBox(row) {
            console.log('+++++',row);
            this.title = "编辑规则"
            this.disabled = false;
            this.ruleId = row.id;
            this.findCourseTagName();
            this.findPublicElectiveByGrade();
            this.getRuleDetails();
            this.curRule = Object.assign({}, row);
            this.showCourseLabelDialog = true;
        },
        addCourseRule(){
            this.showCourseLabelDialog = true;
            this.disabled = false;
            this.title = "新增规则"
            this.courseType.push({edit:true});
            this.courseTag.push({edit:true})
            this.findCourseTagName();
            this.findPublicElectiveByGrade();
            this.curRule ={};
            this.courseType = [{edit:true}];
            this.courseTag = [{edit:true}];
        },
        deleteRule(row){
            this.$confirm("将删除该行数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                 this.deleteCultureRule(row.id);
            });
        },
        deleteCultureRule(id) {
            deleteBclCultureRule(id,this.id)
                .then(res => {
                    if (res.code == 200)
                        this.$message({
                            type: "success",
                            message: "删除成功"
                        });
                    else throw res.code;
                    this.curRule = {};
                    this.queryRules();
                })
                .catch(err => {
                    this.$message({
                        type: "error",
                        message: "删除失败"
                    });
                });
        },
        handleSizeChange(val) {
            this.queryRuleForm.pageSize_ = val;
            this.queryRules();
        },
        handlePageNumChange(val) {
            this.queryRuleForm.pageNum_ = val;
            this.queryRules();
        },
        // splitStr(s) {
        //     return s.split(",");
        // },
        // unpickCourse(tag) {
        //     this.items = this.items.filter(ele => {
        //         return ele.value != tag.value;
        //     });
        // },
        // pickOptionalCourse(val) {
        //     this.totalLang.push(val)
        //     this.totalLang = Array.from(new Set(this.totalLang))

        //     let temp = this.optionalCourses.filter(item => {
        //         return item.value == val;
        //     });
        //     let isRepeat = this.items.some(item => {
        //         return item.value == val;
        //     });
        //     if (temp.length == 1 && !isRepeat) this.items.push(temp[0]);
        //     this.curForeignLang.singleId = "";
        // },
        // queryElectiveCourses(code) {
        //     if (code == undefined || code == null || code == "") {
        //         return;
        //     }
        //     let templateId = this.$route.query.id;
        //     let query = { code: code, templateId: templateId };
        //     queryElectiveCourseList(query).then(res => {
        //         if (res.code == 200) {
        //             let temp = this.trans(res.data);
        //             this.electiveCourses = temp;
        //         } else {
        //             this.electiveCourses = [];
        //             throw res.code;
        //         }
        //     });
        // },
        // trans(list) {
        //     return list.map((ele, i, arr) => {
        //         return { value: ele.code, label: ele.name };
        //     });
        // },
        // labelSelect(val) {
        //     this.curRule.labelId = val.labelId;
        // },
        totalChange(val) {
            this.curRule.totalNum = val;
        },
        optionalChange(val) {
            this.curRule.optional = val;
        },
        clearForm(){
            this.courseTag = [];
            this.courseType = [];
            this.curRule = {
                name:"",
                templateID:"",
                five:"",
                four:"",
                publicElectiveRule:[],
                courseTagRule:[]
            }
        },
        saveCourseRule() {
            this.curRule.publicElectiveRule = []
            this.curRule.courseTagRule = []
            if(!this.curRule.name){
                this.$message({
                    type: "warning",
                    message: "请先填写规则名称"
                });
                return
            }
            let courseType = this.courseType.filter(
                        i => i.edit == true
                    );
            if(courseType.length > 0){
                this.$message({
                    type: "warning",
                    message: "请先保存课程性质"
                });
                return
            }else{
                this.curRule.publicElectiveRule = this.courseType
            }
            let courseTag = this.courseTag.filter(
                        i => i.edit == true
                    );
            if(courseTag.length > 0){
                this.$message({
                    type: "warning",
                    message: "请先保存标签"
                });
                return
            }else{
                this.curRule.courseTagRule = this.courseTag
            }
            if(this.title == "查看规则"){
                this.disabled = false;
                this.showCourseLabelDialog = false;
            }
            if(this.title == "新增规则"){
                this.curRule.templateID = this.$route.query.id
                insertBclCultureRule(this.curRule).then(res => {
                    if (res.code == 200) {
                        this.$message({ type: "success", message: "新增成功" });
                        this.queryRules();
                        this.clearForm();
                        this.showCourseLabelDialog = false;
                    } else throw res.code;
                })
            }
            if(this.title == "编辑规则"){
                this.curRule.id = this.ruleId
                this.curRule.templateID = this.$route.query.id
                updateBclCultureRule(this.curRule).then(res => {
                    if (res.code == 200) {
                        this.$message({ type: "success", message: "编辑成功" });
                        this.queryRules();
                        this.clearForm();
                        this.showCourseLabelDialog = false;
                    } else throw res.code;
                })
            }
            
            // if(rule.id){
            //     rule.limitType = this.limitType;
            //     this.updateCultureRule();
            // }else{
            //    rule.checkedNum=this.checkedNum;
            //     rule.checkedCredits=this.checkedCredits;
            //     this.inserBclCultureRex();
            // }
        },
        updateCultureRule() {
            let parms={
                minNum:this.curRule.minNum,
                maxNum:this.curRule.maxNum,
                expression:this.curRule.expression,
                minCredits:this.curRule.minCredits,
                id:this.curRule.id
            }
            updateCultureRex(parms)
                .then(res => {
                    if (res.code == 200) {
                        this.$message({ type: "success", message: "更新成功" });
                        this.queryRules();
                        this.showCourseLabelDialog = false;
                    } else throw res.code;
                })
                .catch(err => {
                    console.log(err);
                    this.$message({ type: "error", message: "更新失败" });
                });
        },
        // 新增
        inserBclCultureRex(){
            console.log('新增',this.curRule);
            
            let parms={
                labelId:this.curRule.label,
                trainingId:this.queryRuleForm.taringingId,
                minNum:this.curRule.optional,
                maxNum:this.curRule.totalNum,
                expression:this.curRule.expression,
                minCredits:this.curRule.minCredits,
                id:this.curRule.id
            }
            inserBclCultureRex(parms).then(res=>{
                this.queryRules();
                this.showCourseLabelDialog = false
                console.log('tianjia',res);
                
            })
        },
        queryRules() {
            findBclCultureRule(this.queryRuleForm)
                .then(res => {
                    if (res.code == 200) {
                        this.rulesList = res.data.list;
                        console.log('wwww',this.rulesList);
                        this.queryRuleForm.total = res.data.total_;
                    } else throw res.code;
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // deleteLine(id) {
        //     if (!id) return;
        //     this.$confirm("将删除该行数据, 是否继续?", "提示", {
        //         confirmButtonText: "确定",
        //         cancelButtonText: "取消",
        //         type: "warning"
        //     }).then(() => {
        //          this.deleteCultureRule(id);
        //     });
        // },
        // deleteCultureRule(id) {
        //     deleteCultureRule({ id: id })
        //         .then(res => {
        //             if (res.code == 200)
        //                 this.$message({
        //                     type: "success",
        //                     message: "删除成功"
        //                 });
        //             else throw res.code;
        //             this.curRule = {};
        //             this.queryRules();
        //         })
        //         .catch(err => {
        //             this.$message({
        //                 type: "error",
        //                 message: "删除失败"
        //             });
        //         });
        // },
        ok(){
            this.$store.commit("delete_tabs", this.$route.path);
            this.$router.push({
                    path: "/bclCultureSchemeTemplates",
                });
        }
    },
    // watch: {
    //     activeTab: function(_new, _old) {},
    // }
};
</script>

<style lang="scss" scoped>
.coursemap {
    font-size: 12px;
    margin-bottom: 15px;
}
.new.edu-page-title {
    margin-bottom: 0;
    span {
        cursor: pointer;
        display: inline-block;
        padding-right: 20px;
    }
}

</style>

